#include "Shader.h"

#include "Object/NewObject.h"

#include "File/FileUtils.h"

#include "Render/Material/RenderShader.h"
#include "Render/RenderContext.h"

RShader::RShader()
{
    RenderShader=std::make_shared<CRenderShader>();
}

RShader::~RShader()
{

}

void RShader::UpdateShaderData(TVector<uint8_t> &&SpvFileData, EShaderStage ShaderType)
{
    Data=std::move(SpvFileData);
    this->ShaderType=ShaderType;

    SyncToRenderThread();
}

void RShader::PostLoadFromAsset()
{
    SyncToRenderThread();
}

void RShader::SyncToRenderThread()
{
    RenderShader->SetShaderData(Data,this->ShaderType);
}

TSharedObjectPtr<RShader> RShader::CreateShader(TVector<uint8_t> &&SpvFileData, EShaderStage ShaderType)
{
    auto Ret = NewObject<RShader>();
    Ret->UpdateShaderData(std::move(SpvFileData),ShaderType);
    return Ret;
}

TSharedObjectPtr<RShader> RShader::CreateShader(const TVector<uint8_t> &SpvFileData, EShaderStage ShaderType)
{
    auto Ret = NewObject<RShader>();
    Ret->UpdateShaderData(TVector<uint8_t>(SpvFileData),ShaderType);
    return Ret;
}

TSharedObjectPtr<RShader> RShader::CreateShader(const SPath &SpvFile, EShaderStage ShaderType)
{
    return CreateShader(SFileUtils::ReadToBinary(SpvFile),ShaderType);
}